package model
{
	import command.QuestionsManagerCommand;
	
	import event.TriviaEvent;
	
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import vo.Question;
	import vo.QuestionAnswer;
	
	[Bindable]
	public class ApplicationModel	
	{
		public  var correct:Number = 0;
		public  var incorrect:Number = 0;
		public  var notAnswers:Number = 0;
		public var timeInSeconds:Number=60;
		public var clockCounter:Timer;
		public  var userID:String = "1356288022";
	
		private var _questions:ArrayCollection;
		private var _currentCuestion:Question;
		private var _questionsResolved:ArrayCollection= new ArrayCollection();
		private var _currentIndex:int ;
		private  var _levels:ArrayCollection;
		private var _scoring:int;
		public  var level:Number=99;
		
		public var URL_LEVEL:String = "http://localhost:8080/trivia-ucalp-back/getLevels";
		public var URL_SEND_RESULT:String = "http://localhost:8080/trivia-ucalp-back/saveScore";
		public var URL_QUESTIONS:String = "http://localhost:8080/trivia-ucalp-back/getQuestions";
		public var URL_AUTHENTICATE_USER:String = "http://localhost:8080/trivia-ucalp-back/getLoggedUserId";
		public var URL_SAVE_STAT:String = "http://localhost:8080/trivia-ucalp-back/saveStats";
		
		static private var _instance:ApplicationModel;
		
		public function get scoring():int
		{
			if (!_scoring)
			{
				return 0;
			}
			return _scoring;	
		}
		public function set scoring(value:int)
		{
			_scoring = value;
		}
		
		public function get levels():ArrayCollection
		{
			if (!_levels)
			{
				_levels = new ArrayCollection();
				(new TriviaEvent(TriviaEvent.TRIVIA_LEVELS)).dispatch();
			}
			return _levels;	
		}
		public function set levels(value:ArrayCollection):void
		{
			_levels = value;
		}
		
		static public function get instance():ApplicationModel
		{
			if (!_instance)
				_instance = new ApplicationModel();
			
			return _instance;
		}

		
		public function nextQuestion ():Boolean
		{
			_currentIndex++;
			if (questions.length>(currentIndex))
			{
				currentCuestion = (questions.getItemAt(_currentIndex,0)) as Question;
				return true;
			}
			currentCuestion = null;
			return false;
		}
		
		public function ApplicationModel()
		{
			

		}
		public function get currentIndex():int
		{
			return _currentIndex;
		}
		public function set currentIndex(value:int):void
		{
			_currentIndex = value;
		}
		public function get questions():ArrayCollection
		{
			return _questions;
		}
		public function set questions(value:ArrayCollection):void
		{
			_questions = value;
		}
		public function get questionsResolved():ArrayCollection
		{
			return _questionsResolved;
		}
		public function set questionsResolved(value:ArrayCollection):void
		{
			_questionsResolved = value;
		}
		
		public function get currentCuestion():Question
		{
			return _currentCuestion;
		}
		public function set currentCuestion(value:Question):void
		{
			_currentCuestion = value;
		}
		
		public function currentQuestionCorrect (pBoolean:Boolean,pSolucionSelected:String,pResponseSelection:Boolean):void
		{
			var questionResult:QuestionAnswer = new QuestionAnswer();
			questionResult.id = currentCuestion.id;
			questionResult.solutionSelected = pSolucionSelected; 
			questionResult.statement = currentCuestion.statement;
			questionResult.correct = pBoolean;
			questionResult.responseSelection = pResponseSelection;
			questionsResolved.addItem(questionResult);
			
			if (!pResponseSelection)
			{
				this.notAnswers++;
			}
			else
			if (pBoolean)
			{
				//Alert.show("Congratulations Correct Answer!\n"+"Tip:"+_currentCuestion.helpmessage,"Information");
				///Alert.show("Respuesta correcta!\n");
				this.correct++;
			}
			else
			{
				//Alert.show("The answer is incorrect!\n"+"Tip:"+_currentCuestion.helpmessage,"Information");
				//Alert.show("Respuesta incorrecta. Ops!\n");
				this.incorrect++;
			}
		}
		public function reStart():void
		{
				_currentCuestion = null;
				_currentIndex = -1;
				questionsResolved = new ArrayCollection();
				questions = new ArrayCollection();
				this.correct = 0;
				this.incorrect = 0;
				this.notAnswers = 0;
		}
			

	}
}